package com.woniuxy.rateassembly.apiimpl;


import com.aliyun.oss.OSS;
import com.woniuxy.api.CommentAPI;
import com.woniuxy.common.Result;
import com.woniuxy.form.CommentForm;
import com.woniuxy.rateassembly.model.Comment;
import com.woniuxy.rateassembly.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zz
 * @since 2021-05-27
 */
@RestController
@Slf4j
public class CommentController implements CommentAPI {

    @Autowired
    private ICommentService commentService;

    //用户评价（新增酒店评论）
    @Override
    public Result addComment(CommentForm commentForm) {

        log.info("传过来的评论是{}",commentForm);
        commentService.addComment(commentForm);
        return Result.success();
    }

    //用户查看评论
    @Override
    public Result findCommentByUserId(Integer userId) {
        List<Comment> comments = commentService.findCommentByUserId(userId);
        log.info("查询到用户id为{}的所有评论{}",userId,comments);
        return Result.success(comments);
    }

    //商家查看评论
    @Override
    public Result findCommentByHotelId(Integer hotelId) {
        List<Comment> comments = commentService.findCommentByHotelId(hotelId);
        log.info("查询到酒店id为{}的所有评论{}",hotelId,comments);
        return Result.success(comments);
    }

    //删除评论
    @Override
    public Result delectCommentById(Integer commentId) {
        log.info("要删除的评论id为{}",commentId);
        commentService.delectCommentById(commentId);
        return Result.success("删除成功");
    }


    @Autowired
    private OSS ossCli;
    //文件上传
    @Override
    public Result upload(MultipartFile poster) {
        String originalFilename = poster.getOriginalFilename();

        long millis = System.currentTimeMillis();

        String filename= millis+originalFilename;
        try {
            ossCli.putObject("hotel-rate",filename,poster.getInputStream());
            return Result.success("httpsw://hotel-rate.oss-cn-chengdu.aliyuncs.com/"+filename);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

